package top.mable.payment.controller;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import top.mable.common.vo.*;
import top.mable.common.exception.BusinessException;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/exception")
    public Result testException(@RequestBody RequestData<RequestExceptionDTO> requestData){
        RequestExceptionDTO requestExceptionDTO = requestData.getData();
        if (requestExceptionDTO.getName() == null){
            throw new BusinessException(ExceptionEnum.IS_NULL,new String[]{requestData.getReqId()});
        }
        return Result.success(requestExceptionDTO);
    }


    @RequestMapping("/getKey1")
    public Result getKey1(@RequestBody Map map){
        String key  = (String) map.get("key");
        String str = (String) redisTemplate.opsForValue().get(key);
        //map.put("key",str);
        return Result.success(str);
    }

    @RequestMapping("/request")
    public void request(){
        GpTradeInfoDTO dto = new GpTradeInfoDTO();
        dto.setId("1");
        HttpEntity entity  = new HttpEntity(dto);
        ResponseEntity<String> result = restTemplate.exchange("http://localhost:8991/getOneTradeInfo", HttpMethod.POST, entity, String.class);
        JSONObject data = JSONObject.parseObject(result.getBody()).getJSONObject("data");
        log.info("request 请求结果{}",data);
    }

}
